_SAVE_CAR = {
  /**
   * change the button display for the items in cart
  **/
  itemIsSaved : function (id, isSaved)
  {
     $('.save-car-'+id).removeClass('btn-info');
     $('.save-car-'+id).removeClass('btn-danger');

	 if (isSaved)
	 {
       $('.save-car-'+id).html('Saved');
       $('.save-car-'+id).addClass('btn-primary');
	 }
	 else
	 {
       $('.save-car-'+id).html('Save <i class="icon-white icon-star"></i>');
       $('.save-car-'+id).addClass('btn-info');
	 }
  },
  /**
   * get ajax response and update items 
  **/
  ajaxResponse : function (data)
  {
    for (id in data)
  	  _SAVE_CAR.itemIsSaved(id, true);
  },

  /**
   * add item to the cart 
  **/
  saveItem : function (id)
  {
	 $.ajax({
		type: "POST",
	    url: "/saved-vouchers/add",
		dataType : 'json',
		data: { 'id' : id },
		success : function (data) {
		  _SAVE_CAR.ajaxResponse(data);
		}
	})
  },
  removeItem : function (id)
  {
	 $.ajax({
		type: "POST",
	    url: "/saved-vouchers/del",
		dataType : 'json',
		data: { 'id' : id },
		success : function (data) {
		  _SAVE_CAR.ajaxResponse(data);
		}
	})
  },
  save_remove_toggle : function (id)
  {
     if ($('.save-car-'+id).html() == 'Saved')
	 {
	   _SAVE_CAR.itemIsSaved(id, false)
	   _SAVE_CAR.removeItem(id); 
	 }
	 else
	 {
	   _SAVE_CAR.itemIsSaved(id, true)
	   _SAVE_CAR.saveItem(id); 
	 }

  },
  /**
   * get current cart items from the server
  **/
  refresh : function ()
  {
	 $.ajax({
		type: "POST",
	    url: "/saved-vouchers/refresh",
		dataType : 'json',
		success : function (data) {
		  _SAVE_CAR.ajaxResponse(data);
		}
	})
  }
}

$(function () {
  $(".save-car").click( function () {
    _SAVE_CAR.save_remove_toggle( $(this).attr('vID') );
  });
  
  _SAVE_CAR.refresh( );
})

